#include <dbg.h>

#include <cassert>

using namespace std;

class Solution {
public:
    int numberOfSubarrays(vector<int>& nums, int k) {
        int res = 0;
        int cnt1 = 0;
        int cnt2 = 0;
        for (int left1 = 0, left2 = 0, right = 0; right < nums.size();
             ++right) {
            cnt1 += (nums[right] & 1);  // 统计大于等于k个奇数的窗口的奇数个数
            while (cnt1 >= k && left1 <= right) {
                cnt1 -= (nums[left1] & 1);
                ++left1;
            }
            cnt2 += (nums[right] & 1);  // 统计大于k个奇数的窗口的奇数个数
            while (cnt2 > k && left2 <= right) {
                cnt2 -= (nums[left2] & 1);
                ++left2;
            }
            res += (left1 - left2);  // 两者相减即为恰好k个奇数的子数组个数
        }
        return res;
    }
};

int main() {
    Solution s;

    vector<int> nums1{1, 1, 2, 1, 1};
    vector<int> nums2{2, 4, 6};
    vector<int> nums3{2, 2, 2, 1, 2, 2, 1, 2, 2, 2};

    int k1 = 3;
    int k2 = 1;
    int k3 = 2;

    // assert(s.numberOfSubarrays(nums1, k1) == 2);
    // assert(s.numberOfSubarrays(nums2, k2) == 0);
    assert(s.numberOfSubarrays(nums3, k3) == 16);

    return 0;
}